package com.hekang.activitlifecycle;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hekang.activitlifecycle.bean.User;

public class MainActivity extends AppCompatActivity {

    public static final String BUT_TITLE = "but_title";
    public static final String BUNDLE_EXTRA = "bundle_Extra";
    private EditText val;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

        Button button = (Button) findViewById(R.id.bnt);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开始跳转
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, OthActivity.class);
                //第一种传值方式
                //传普通字符串
                //intent.putExtra(BUT_TITLE, val.getText().toString());

                //传String数组
                //intent.putExtra(BUT_TITLE, new String[]{"A", "B", "C"});

                //第二种传值方式   bundle和Map差不多
//                Bundle bundle = new Bundle();
//                bundle.putString(BUT_TITLE, val.getText().toString());
//                intent.putExtra(BUNDLE_EXTRA,bundle);

                //第三种传值方式   序列化
                //intent.putExtra(BUNDLE_EXTRA,new User());

                startActivityForResult(intent, 999);
            }
        });
    }

    //监听上一个页面返回


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 999 && resultCode == RESULT_OK) {
            if (data != null) {
                String msg = data.getStringExtra("msg");
                Toast.makeText(this, msg+"--"+requestCode, Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 初始化控件
     */
    private void init() {
        val = (EditText) findViewById(R.id.val);
    }
}
